package com.mvp.shq.washer.ui.news;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.NewsAdapter;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.entity.MessageBean;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;
import com.mvp.shq.washer.view.other.AppBarStateChangeListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NewsActivity extends BaseActivity<NewsPresenter, NewsModel> implements NewsConstract.View {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.img_block)
    ImageView imgBlock;
    @BindView(R.id.tv_not_data)
    TextView tvNotData;
    @BindView(R.id.arl_not_data)
    AutoRelativeLayout arlNotData;
    @BindView(R.id.appbar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.all_not_login)
    AutoLinearLayout allNotLogin;
    @BindView(R.id.tv_to_login)
    TextView tvToLogin;
    private List<MessageBean> messages;
    private NewsAdapter adapter;
    private int pageNo = 1;
    private int pageSize = 10;
    private boolean isLoadMore = false;
    private boolean hasMoreData = true;

    @Override
    public int getLayoutId() {
        return R.layout.activity_news;
    }

    @Override
    public void initView() {
        title.setText("系统消息");
        title.setVisibility(View.GONE);
        recyclerview.setLayoutManager(new LinearLayoutManager(context));
        RecyclerViewDivider divider = new RecyclerViewDivider(context, LinearLayoutManager.HORIZONTAL, 2, ContextCompat.getColor(context, R.color.new_item));
        recyclerview.addItemDecoration(divider);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                //showLoadingDialog(true);
                pageNo = 1;
                isLoadMore = false;
                hasMoreData = true;
                presenter.getMsgs(pageNo, pageSize);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (!hasMoreData){
                    refreshlayout.finishLoadmore(2000);
                    return;
                }
                pageNo++;
                isLoadMore = true;
                presenter.getMsgs(pageNo, pageSize);
            }
        });
        appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if( state == State.EXPANDED ) {

                    //展开状态
                    title.setVisibility(View.GONE);

                }else if(state == State.COLLAPSED){

                    //折叠状态
                    title.setVisibility(View.VISIBLE);

                }else {

                    //中间状态
                    title.setVisibility(View.GONE);

                }
            }
        });
        refreshLayout.setEnableAutoLoadmore(false);
        if (SpUtil.getUser() == null){
            allNotLogin.setVisibility(View.VISIBLE);
            arlNotData.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.GONE);
        }else {
            allNotLogin.setVisibility(View.GONE);
            arlNotData.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.VISIBLE);
            refreshLayout.autoRefresh();
        }
        tvNotData.setText("暂无消息");
        imgBlock.setImageResource(R.mipmap.default_page_massage);
    }


    @OnClick({R.id.back,R.id.tv_to_login})
    public void onViewClicked(View view)
    {
        switch (view.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.tv_to_login:
                JumpUtil.toLogin(context);
                finish();
                break;
        }

    }

    @Override
    public void msgSuccess(List<MessageBean> msg) {
        if (!isLoadMore) {
            SpUtil.setUnReadNum(0);
            RxBus.get().send(EventCode.SET_UNREAD_MSG,0);
            refreshLayout.finishRefresh();
            if (msg == null) {
                return;
            }
            messages = msg;
            adapter = new NewsAdapter(context, messages);
            recyclerview.setAdapter(adapter);
        } else {
            refreshLayout.finishLoadmore();
            if (msg == null) {
                return;
            }
            messages.addAll(msg);
            adapter.notifyDataSetChanged();
        }
        if (msg.size() < pageSize){
            hasMoreData = false;
        }
        hasData();
    }

    @Override
    public void msgFail(String msg) {
        T.show(msg);
        hasData();
    }

    private void hasData(){
        if (messages!= null && messages.size() > 0){
            recyclerview.setVisibility(View.VISIBLE);
            arlNotData.setVisibility(View.GONE);
        }else {
            recyclerview.setVisibility(View.GONE);
            arlNotData.setVisibility(View.VISIBLE);
        }
    }
}
